package common.util.excel;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zyn@winning.com.cn
 * @Description:
 * @Date: 2018/8/15 9:02
 **/
public class DateConvert {
    public DateConvert() {
    }

    public static String getNowDateYYYY_MM_DD_HHMMSS_SSS() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                YYYY_MM_DD_HHMMSS_SSS);
        return dateFormat.format(new Date());
    }

    public static boolean checkDate(String sourceDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        if (sourceDate == null || sourceDate.trim().length() == 0)
            return false;
        try {
            dateFormat.setLenient(false);
            dateFormat.parse(sourceDate);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static Date convert(String dateStr, String dateFormat) {
        SimpleDateFormat sFormat = new SimpleDateFormat(dateFormat);
        try {
            return sFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convert(Date date, String dateFormat) {
        SimpleDateFormat sFormat = new SimpleDateFormat(dateFormat);
        return sFormat.format(date);
    }

    public static boolean checkDateYYYYMMDD(String sourceDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYMMDD);
        if (sourceDate == null || sourceDate.trim().length() == 0)
            return false;
        try {
            dateFormat.setLenient(false);
            dateFormat.parse(sourceDate);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static Date StrToDateYYYYMMDD(String str) {
        SimpleDateFormat format = new SimpleDateFormat(YYYMMDD);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date StrToDateYYYY_MM_DD(String str) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date StrToDateYYYY_MM_DD_HH_MM_SS(String str) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getDateYYYY_MM_DD(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            return getDateYYYY_MM_DD(sdf.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateYYYY_MM_DD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateYYYY_MM_DD(String date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return getDateYYYY_MM_DD(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateYYYY_MM_DD_HH_MM_SS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateYYYYMMDD(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYMMDD);
        try {
            return getDateYYYYMMDD(sdf.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateYYYYMMDD(String date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return getDateYYYYMMDD(sdf.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateYMD(String date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return getDateYYYYMMDD(sdf.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateYYYYMMDD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYMMDD);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateYYYYMMDDHHMMDD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD_HHMMSS);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateYMD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateYMDHMD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateHH_MM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDateHH(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getDayOfWeek(Date date) {
        String dayNames[] = { "\u661F\u671F\u65E5", "\u661F\u671F\u4E00",
                "\u661F\u671F\u4E8C", "\u661F\u671F\u4E09",
                "\u661F\u671F\u56DB", "\u661F\u671F\u4E94",
                "\u661F\u671F\u516D" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(7);
        return dayNames[dayOfWeek - 1];
    }

    public static String getDayOfWeek(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Date myDate = null;
        try {
            myDate = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String dayNames[] = { "\u661F\u671F\u65E5", "\u661F\u671F\u4E00",
                "\u661F\u671F\u4E8C", "\u661F\u671F\u4E09",
                "\u661F\u671F\u56DB", "\u661F\u671F\u4E94",
                "\u661F\u671F\u516D" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(myDate);
        int dayOfWeek = calendar.get(7);
        return dayNames[dayOfWeek - 1];
    }

    public static Date addYear(Date source, int years) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(source);
        cal.set(1, cal.get(1) + years);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            date = sdf.parse((new SimpleDateFormat(YYYY_MM_DD)).format(cal
                    .getTime()));
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addMonth(Date source, int months) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(source);
        cal.add(2, months);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            date = sdf.parse((new SimpleDateFormat(YYYY_MM_DD)).format(cal
                    .getTime()));
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addDay(Date source, int days) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(source);
        cal.set(5, cal.get(5) + days);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            date = sdf.parse((new SimpleDateFormat(YYYY_MM_DD)).format(cal
                    .getTime()));
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getNowYYYY_MM_DD_HH_MM_DD() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowYYYY_MM_DD() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowYYYYMMDDHHMMDD() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD_HHMMSS);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowYYYYMMDD() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYMMDD);
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowYMDHMS() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowYMD() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getNowOfWeek() {
        return getDayOfWeek(new Date());
    }

    public static Date addMonthNow(int months) {
        return addMonth(new Date(), months);
    }

    public static Date addNow(int days) {
        return addDay(new Date(), days);
    }

    public static java.sql.Date getSqlDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        String str = sdf.format(date);
        return java.sql.Date.valueOf(str);
    }

    public static String getAddDayNowYYYY_MM_DD_HH_MM_DD(int days) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        return sdf.format(Long.valueOf(addNow(days).getTime()));
    }

    public static String getNowYYYYMMDD_HHMMDD_() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd/HHmmss/");
        return sdf.format(Long.valueOf(date.getTime()));
    }

    public static String getForStringYYYY_MM_DD_HH_MM_DD(String date) {
        String dateForString = "";
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        try {
            Date myDate = sdf.parse(date);
            dateForString = getDateYYYY_MM_DD_HH_MM_SS(myDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateForString;
    }

    public static long getQuot(String time1, String time2) {
        long quot = 0L;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date date1 = ft.parse(time1);
            Date date2 = ft.parse(time2);
            quot = date1.getTime() - date2.getTime();
            quot = quot / 1000L / 60L / 60L / 24L;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return quot;
    }

    public static Date getDateByString(String birthdayString) {
        Date birthday = new Date();
        try {
            birthdayString = birthdayString.replaceAll("\u5E74", "-");
            birthdayString = birthdayString.replaceAll("\u6708", "-");
            birthdayString = birthdayString.replaceAll("\u65E5", "");
            birthdayString = (new StringBuilder("20")).append(birthdayString)
                    .toString();
            SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
            birthday = sdf.parse(birthdayString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return birthday;
    }

    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay))
            return 0;
        int yearNow = cal.get(1);
        int monthNow = cal.get(2);
        int dayOfMonthNow = cal.get(5);
        cal.setTime(birthDay);
        int yearBirth = cal.get(1);
        int monthBirth = cal.get(2);
        int dayOfMonthBirth = cal.get(5);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth)
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        return age;
    }

    public static int getAge(String strBirthDay, String format)
            throws Exception {
        DateFormat df = new SimpleDateFormat(format);
        Date birthDay = df.parse(strBirthDay);
        return getAge(birthDay);
    }

    public static int getDayOfYear(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date myDate = null;
        try {
            myDate = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(myDate);
            int year = calendar.get(1);
            return year;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getDayOfMonth(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date myDate = null;
        try {
            myDate = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(myDate);
            int month = calendar.get(2) + 1;
            return month;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getDayOfDay(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date myDate = null;
        try {
            myDate = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(myDate);
            int date = calendar.get(5);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getDateStrToStr(String dateString, String formatStr) {
        String result = "";
        try {
            Date testDate = new Date(dateString);
            SimpleDateFormat sdfToStr = new SimpleDateFormat(formatStr);
            result = sdfToStr.format(testDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String formatDuring(long mss) {
        long hours = mss / 3600000L;
        long minutes = (mss % 3600000L) / 60000L;
        long seconds = (mss % 60000L) / 1000L;
        return (new StringBuilder(String.valueOf(hours)))
                .append("\u5C0F\u65F6").append(minutes).append("\u5206")
                .append(seconds).append("\u79D2").toString();
    }

    public static Map getDate() {
        Map map = new HashMap();
        Calendar date = Calendar.getInstance();
        date.set(1, date.get(1));
        date.set(2, date.get(2));
        date.set(5, 1);
        String reDate = getDateYYYY_MM_DD(date);
        map.put("kssj", reDate);
        date.setTime(new Date());
        date.set(5, 1);
        date.add(2, 1);
        date.add(5, -1);
        reDate = getDateYYYY_MM_DD(date);
        map.put("jssj", reDate);
        map.put("nf", getDateYYYY(date));
        return map;
    }

    public static String getDateYYYY_MM_DD(Calendar date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        return sdf.format(date.getTime());
    }

    public static String getDateYYYY(Calendar date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date.getTime());
    }

    public static String timeCompareTo(Date date) {
        DateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        String reDate = null;
        Date dt1 = null;
        try {
            dt1 = sdf.parse((new StringBuilder(String
                    .valueOf(getDateYYYY_MM_DD(date)))).append(" 00:00:00")
                    .toString());
            if (dt1.getTime() == date.getTime())
                reDate = (new StringBuilder(String
                        .valueOf(getDateYYYY_MM_DD(date)))).append("23:59:59")
                        .toString();
            else
                reDate = getDateYYYY_MM_DD_HH_MM_SS(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return reDate;
    }

    public static int timeCompare(Date date1, Date date2) {
        int flag = 0;
        try {
            if (date1.getTime() > date2.getTime())
                flag = 1;
            else if (date1.getTime() < date2.getTime())
                flag = -1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static Date addHours(Date sourceDate, int hours) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(sourceDate);
        cal.add(11, hours);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HHMMSS);
        try {
            date = sdf.parse((new SimpleDateFormat(YYYY_MM_DD_HHMMSS))
                    .format(cal.getTime()));
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Long getID() {
        long day = System.currentTimeMillis();
        return Long.valueOf(day);
    }

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYMMDD = "yyyyMMdd";
    public static String YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDD_HHMMSS = "yyyyMMdd HH:mm:ss";
    public static String YYYY_MM_DD_HHMMSS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

}